883G - Orientation of Edges - CodeForces Solution


dfs and similar graphs *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ull unsigned long long
#define inf INT_MAX
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>
#define frs first
#define sec second
#define neparno(x) (x&1)
#define parno(x) (!neparno(x))
#define lowbit(x) (x&-x)
#define nzd __gcd
#define nzs(e1,e2) ((e1*e2)/nzd(e1,e2))
#define yes cout << "Yes\n"
#define no cout << "No\n"
#define yesno(x) cout << ((x)?"Yes\n":"No\n")
#define condcout(x,y,z) cout << ((x)?y:z)
#define all(V) V.begin(),V.end()
#define fill(V,v) fill(all(V),v)
#define l_b(BRL,brl) lower_bound( BRL.begin(), BRL.end(), brl )-BRL.begin()-1
#define u_b(BRU,bru) upper_bound( BRU.begin(), BRU.end(), bru )-BRU.begin()
#define mod 1000000007
#define mxn 2000002

struct grana
{
    ll v;
    ll t;
    ll o;
    ll i;
};

ll q,n,m,s,R[mxn],T[mxn],U[mxn],VV[mxn];
bool V[mxn];
vector <grana> G[mxn];

ll DFS_MAX( ll i )
{
    V[i] = true;
    for ( auto x : G[i] )
    {
        ll next = x.v;
        if ( V[next] )
        {
            continue;
        }
        R[x.i] = x.o;
        DFS_MAX(next);
    }
}

ll DFS_MIN( ll i )
{
    V[i] = true;
    for ( auto x : G[i] )
    {
        ll next = x.v;
        if ( V[next] || x.t == 2 )
        {
            continue;
        }
        R[x.i] = x.o;
        DFS_MIN(next);
    }
}

ll Izbroj()
{
    ll ret = 0;
    for ( ll i = 1; i <= n; i++ )
    {
        ret += V[i];
    }
    return ret;
}

void Set()
{
    for ( ll i = 1; i <= n; i++ )
    {
        V[i] = false;
    }
}

int main()
{
    ios;

    cin >> n >> m >> s;
    ll u,v,t;
    for ( ll i = 1; i <= m; i++ )
    {
        cin >> t >> u >> v;
        T[i] = t; U[i] = u; VV[i] = v;
        if ( t == 1 )
        {
            G[u].push_back({v,1,-1,i});
        }
        else
        {
            G[u].push_back({v,2,0,i});
            G[v].push_back({u,2,1,i});
        }
    }

    DFS_MAX(s);
    cout << Izbroj() << "\n";
    for ( ll i = 1; i <= m; i++ )
    {
        if ( T[i] == 2 )
        {
            condcout(R[i],"-","+");
        }
    }
    cout << "\n";

    Set();

    DFS_MIN(s);
    cout << Izbroj() << "\n";
    for ( ll i = 1; i <= m; i++ )
    {
        if ( T[i] == 2 )
        {
            if ( V[U[i]] && !V[VV[i]] )
            {
                R[i] = 1;
            }
            if ( !V[U[i]] && V[VV[i]] )
            {
                R[i] = 0;
            }
            condcout(R[i],"-","+");
        }
    }
    cout << "\n";

    return 0;
}


Comments

Submit
0 Comments
More Questions

1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year